package com.cai.baselibrary

import android.app.Application
import android.content.Context
import androidx.multidex.MultiDex
import com.alibaba.android.arouter.launcher.ARouter
import com.cai.baselibrary.net.intercepto.LoggerInterceptor
import com.lzy.okgo.OkGo
import com.lzy.okgo.cache.CacheEntity
import com.lzy.okgo.cache.CacheMode
import com.lzy.okserver.OkDownload
import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit


open class BaseApplication : Application() {
    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    override fun onCreate() {
        super.onCreate()
        initHttp()
        initARouter()
    }

    /**全局路由器初始化*/
    private fun initARouter() {
        if (BuildConfig.DEBUG) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(this)
    }


    /**初始化请求*/
    private fun initHttp() {
        val builder = OkHttpClient.Builder()
//        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
////log打印级别，决定了log显示的详细程度
//        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
////log颜色级别，决定了log在控制台显示的颜色
//        loggingInterceptor.setColorLevel(Level.INFO);
//        builder.addInterceptor(loggingInterceptor);
        //        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
////log打印级别，决定了log显示的详细程度
//        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
////log颜色级别，决定了log在控制台显示的颜色
//        loggingInterceptor.setColorLevel(Level.INFO);
//        builder.addInterceptor(loggingInterceptor);
        builder.addInterceptor(LoggerInterceptor("TAG",true))
        builder.connectTimeout(10000L, TimeUnit.MILLISECONDS)
        builder.readTimeout(10000L, TimeUnit.MILLISECONDS)
        OkGo.getInstance().init(this) //必须调用初始化
            .setOkHttpClient(builder.build()) //建议设置OkHttpClient，不设置将使用默认的
            .setCacheMode(CacheMode.NO_CACHE) //全局统一缓存模式，默认不使用缓存，可以不传
            .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE).retryCount =
            3 //全局统一超时重连次数，默认为三次，那么最差的情况会请求4次(一次原始请求，三次重连请求)，不需要可以设置为0

//                .addCommonHeaders(headers)                      //全局公共头
//                .addCommonParams(params);                       //全局公共参数

        //                .addCommonHeaders(headers)                      //全局公共头
//                .addCommonParams(params);                       //全局公共参数
//        OkDownload.getInstance().folder = ConstantsPath.DOWNLOAD   //下载地址
//        OkDownload.getInstance().threadPool.setCorePoolSize(4)    //下载数目
    }
}